package io.zhengsh.vvip.jvm.proxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class OrderServiceHandler implements InvocationHandler {

    private Object orderService;

    public OrderServiceHandler(Object orderService) {
        this.orderService = orderService;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] args) throws Throwable {
        System.out.println("before !!!");
        Object invoke = method.invoke(orderService, args);
        System.out.println("after  !!!");
        return invoke;
    }

    //    public static void main(String[] args) {
    //        OrderServiceImpl orderServiceImpl = new OrderServiceImpl();
    //        OrderServiceHandler orderServiceHandler = new OrderServiceHandler(orderServiceImpl);
    //        OrderService orderService1 = (OrderService)Proxy
    //            .newProxyInstance(OrderServiceHandler.class.getClassLoader(), orderServiceImpl.getClass().getInterfaces(),
    //                orderServiceHandler);
    //        orderService1.createOrder(new OrderDto());
    //    }

    public static void main(String[] args) {
        OrderServiceImpl orderServiceImpl = new OrderServiceImpl();
        OrderService orderService = (OrderService)Proxy
            .newProxyInstance(OrderServiceHandler.class.getClassLoader(), orderServiceImpl.getClass().getInterfaces(),
                (o, method, args1) -> {
                    System.out.println("before !!!");
                    Object invoke = method.invoke(orderServiceImpl, args1);
                    System.out.println("after  !!!");
                    return invoke;
                });
        orderService.createOrder(new OrderDto());
    }
}
